package com.i5tong.dbviewerlibrary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.i5tong.dbviewerlibrary.adapter.DbFileRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class TableListAvtivity extends Activity implements DbFileRecyclerViewAdapter.OnDbFileChoose {

    private RecyclerView mRecyclerView;
    private List<String> datas;
    private DbFileRecyclerViewAdapter adapter;
    private String db_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_list_avtivity);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        datas = new ArrayList<>();

        db_name = getIntent().getStringExtra("db_name");
        setTitle(db_name);
        SQLiteDatabase db = openOrCreateDatabase(db_name, Context.MODE_PRIVATE, null);
        Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                datas.add(c.getString(0));
                c.moveToNext();
            }
        }
        adapter = new DbFileRecyclerViewAdapter(this, datas);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void click(String filename) {
        Intent intent = new Intent(this, DataViewerActivity.class);
        intent.putExtra("db_name", db_name);
        intent.putExtra("table_name", filename);
        startActivity(intent);
    }
}
